package org.dbcopy.util;

import org.dbcopy.domain.Table;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * Created by IntelliJ IDEA.
 * User: Turkish
 * Date: 13.03.12
 * Time: 19:11
 * To change this template use File | Settings | File Templates.
 */
public class FileUtils {
    private String fileNameFormat;
    private int fileNumber;

    public FileUtils(String fileNameFormat){
        this.fileNameFormat = fileNameFormat;
    }

    public void saveTable(Table table) {
        File tableFile = null;
        try {
            fileNumber++;
            tableFile = new File(String.format(fileNameFormat, fileNumber, table.getName()));
            if (tableFile.exists()) {
                tableFile.delete();
            }
            tableFile.createNewFile();
            FileWriter writer = new FileWriter(tableFile);
            Iterator<String> columnIterator = table.getColumns().iterator();
            while (columnIterator.hasNext()) {
                writer.append(columnIterator.next().toUpperCase());
                if (columnIterator.hasNext()) {
                    writer.append(",");
                }
            }
            writer.append("\n");
            for (List rows : table.getTableData()) {
                Iterator dataIterator = rows.iterator();
                while (dataIterator.hasNext()) {
                    writer.append(dataIterator.next().toString());
                    if (dataIterator.hasNext()) {
                        writer.append(",");
                    }
                }
                writer.append("\n");
            }
            writer.flush();
            writer.close();
            table.setTableFile(tableFile);             
        } catch (IOException e) {
            System.out.println("Unable to save table -> " + table.getName());
            e.printStackTrace();
        }             
    }

    public void zipTables(List<Table> tableList, String zipPath) {
        try {
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath));
            byte buf[] = new byte[1024];
            for (Table table : tableList) {
                File file = table.getTableFile();
                InputStream in = new BufferedInputStream(new FileInputStream(file));
                zos.putNextEntry(new ZipEntry(file.getName()));
                int len;
                while ((len = in.read(buf)) > 0) {
                    zos.write(buf, 0, len);
                }
                zos.closeEntry();
                in.close();
                file.delete();
            }
            zos.flush();
            zos.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Can not write data into zip -> " + zipPath);
        }
        tableList.clear();
    }
    /*
        TODO:
            - Do not save the files from ZIP;
            - Read appropriate data to Table objects.

            Due date: SUNDAY
     */
    public static List<Table> readTablesFromZip(String zipPath) {
        List<Table> tableList = new ArrayList<Table>();
        try {
            ZipInputStream zis = new ZipInputStream(new FileInputStream(zipPath));
            ZipEntry ze = zis.getNextEntry();
            while (ze != null) {
                FileOutputStream fos = new FileOutputStream(ze.getName());
                BufferedOutputStream targetStream = new BufferedOutputStream(fos);
                BufferedReader reader = new BufferedReader(new InputStreamReader(zis));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(ze.getName() + " : " + line);
                    targetStream.write(line.getBytes());
                    targetStream.write("\n".getBytes());
                }
                targetStream.flush();
                targetStream.close();
                ze = zis.getNextEntry();
            }
            zis.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Can not read data from zip file -> " + zipPath);
        }
        return tableList;
    }
}